package com.hk.web.controller.api.backend;

import com.hk.commons.JsonResult;
import com.hk.commons.ProcessAPI;
import com.hk.commons.enums.FeatureToggleType;
import com.hk.commons.enums.Whether;
import com.hk.commons.exceptions.ValidateException;
import com.hk.web.context.MerchantContext;
import com.hk.web.controller.Routes;
import com.hk.web.controller.api.APIController;
import com.hk.web.domain.entity.Merchant;
import com.hk.web.domain.entity.MerchantFeatureToggle;
import com.hk.web.domain.lifeCycle.factory.MerchantFactory;
import com.hk.web.domain.lifeCycle.repository.MerchantFeatureToggleRepository;
import com.hk.web.domain.lifeCycle.repository.MerchantRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 *
 * @author Antinomy
 * @date 17/11/1
 */
@Api(value = "商户后台模块", description = "商户后台模块")
@RestController
public class MerchantBackEndAPIController extends APIController {

    private Logger logger = LoggerFactory.getLogger(MerchantBackEndAPIController.class);

    @Autowired
    private MerchantRepository merchantRepo;

    @Autowired
    private MerchantFeatureToggleRepository featureToggleRepo;

    @Autowired
    private MerchantContext merchantContext;

    @ApiOperation(value = "创建角色", notes = "输入角色基本信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.MERCHANT_BACKEND_CREATE, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult create(HttpServletRequest request, String merchantName,String merchantUrl) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.checkAndPrepare("merchantName", merchantName);
            validate.checkAndPrepare("merchantUrl", merchantUrl);

            Merchant merchant =  MerchantFactory.create(merchantName,merchantUrl);

            merchant= merchantRepo.saveAndFlush(merchant);

            HashMap result = new HashMap();
            result.put("name",merchant.getName());
            result.put("url",merchant.getUrl());

            jsonResult.result = result;
        };

        return api.process(request);
    }

    @ApiOperation(value = "初始化特性开关", notes = "初始化特性开关", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.API_MERCHANT_BACKEND_CHANGE_FEATURE_TOGGLES, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult changeFeatureToggles(HttpServletRequest request, FeatureToggleType featureToggleType) {

        ProcessAPI api = (jsonResult, validate) -> {
            if(featureToggleType == null)
            {
                throw new ValidateException("特性开关不能为空");
            }

            MerchantFeatureToggle featureToggle = merchantContext.findMerchantFeature(featureToggleType);

            if (featureToggle == null) {
                featureToggle = new MerchantFeatureToggle();
                featureToggle.setFeature(featureToggleType);
                featureToggle.setEnable(Whether.YES);
                featureToggle = featureToggleRepo.saveAndFlush(featureToggle);
                jsonResult.result = featureToggle;
                return;
            }

            if(featureToggle.getEnable().equals(Whether.YES)){
                featureToggle.setEnable(Whether.NO);
            }else {
                featureToggle.setEnable(Whether.YES);
            }

            featureToggle = featureToggleRepo.saveAndFlush(featureToggle);
            jsonResult.result = featureToggle;

        };

        return api.process(request);
    }

}
